
package com.rt.schedulenew.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.rt.schedulebase.dto.DictWorkDateDto;
import com.rt.schedulebase.entity.DictWorkDate;
import com.rt.schedulebase.mapper.DictWorkDateMapper;
import com.rt.schedulenew.api.IDictWorkDateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rt.schedulenew.utils.base.CommonUtil;
import com.rt.schedulenew.utils.util.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class DictWorkDateServiceImpl extends ServiceImpl<DictWorkDateMapper, DictWorkDate> implements IDictWorkDateService {
    @Override
    public boolean insertSelective(DictWorkDate dictWorkDate) {
        return retBool(baseMapper.insert(dictWorkDate));
    }

    @Override
    public boolean deleteByPrimaryKey(String key) {
        return retBool(baseMapper.deleteById(key));
    }

    @Override
    public boolean updateByPrimaryKeySelective(DictWorkDate dictWorkDate) {
        return retBool(baseMapper.updateById(dictWorkDate));
    }

    @Override
    public DictWorkDate selectByPrimaryKey(String key) {
        return baseMapper.selectById(key);
    }

    @Override
    public List<DictWorkDate> getList(DictWorkDateDto dto) {
        QueryWrapper<DictWorkDate> qw = new QueryWrapper<>();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("FLAG", dto.getFlag());
        qw.allEq(map, false);
        if (StringUtils.isNotBlank(dto.getStartDate())) {
            qw.ge("THE_DATE", dto.getStartDate());
        }
        if (StringUtils.isNotBlank(dto.getEndDate())) {
            qw.le("THE_DATE", dto.getEndDate());
        }
        List<DictWorkDate> list = baseMapper.selectList(qw);
        return list;
    }

    @Override
    public List<DictWorkDate> getListAll() {
        return baseMapper.selectList(null);
    }

    @Override
    public boolean delete(DictWorkDate dictWorkDate) {
        UpdateWrapper<DictWorkDate> uw = new UpdateWrapper<>();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("THE_DATE", dictWorkDate.getTheDate());
        map.put("FLAG", dictWorkDate.getFlag());
        uw.allEq(map, false);
        return retBool(baseMapper.delete(uw));
    }

    @Override
    public boolean update(DictWorkDate dictWorkDate) {
        UpdateWrapper<DictWorkDate> uw = new UpdateWrapper<>();
        Map<String, Object> map = new HashMap<>();
        map.put("THE_DATE", dictWorkDate.getTheDate());
        uw.allEq(map, false);
        uw.set(StringUtils.isBlank(dictWorkDate.getMemo()), "MEMO", "");
        return retBool(baseMapper.update(dictWorkDate, uw));
    }

    @Override
    public DictWorkDate getInfo(DictWorkDateDto dto) {
        QueryWrapper<DictWorkDate> qw = new QueryWrapper<>();
        Map<String, Object> map = new HashMap<>();
        qw.allEq(map, false);
        return baseMapper.selectOne(qw);
    }

    @Override
    public boolean insertSelectiveBatch(DictWorkDate dictWorkDate) {
        List<String> theDates = CommonUtil.split2List(dictWorkDate.getTheDate());
        for (String theDate : theDates) {
            dictWorkDate.setTheDate(theDate);
            baseMapper.insert(dictWorkDate);
        }
        return true;
    }

    @Override
    public boolean updateBatch(DictWorkDateDto dto) {
        List<String> theDates = CommonUtil.split2List(dto.getOldTheDate());
        for (String theDate : theDates) {
            DictWorkDate dictWorkDate = new DictWorkDate();
            dictWorkDate.setTheDate(theDate);
            dictWorkDate.setMemo(dto.getMemo());
            dictWorkDate.setFlag(dto.getFlag());
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("THE_DATE", theDate);
            UpdateWrapper<DictWorkDate> uw = new UpdateWrapper<>();
            uw.allEq(map, false);
            uw.set(StringUtils.isBlank(dto.getMemo()), "MEMO", "");
            baseMapper.update(dictWorkDate, uw);
        }
        return true;
    }

    @Override
    public boolean insertAuto(DictWorkDateDto dto) {
        int year = Integer.parseInt(dto.getTheYear());
        Calendar now = Calendar.getInstance();
        List<DictWorkDate> list = new ArrayList<DictWorkDate>();
        for (int month = 0; month < 12; ++month) {
            now.set(year, month, 1);
            for (int month_day = now.getActualMaximum(Calendar.DATE), i = 1; i < month_day + 1; ++i) {
                String theDate = DateUtil.getLunarDate(year, month, i);
                if (theDate != null) {
                    DictWorkDateDto dictWorkDateDto = new DictWorkDateDto();
                    dictWorkDateDto.setTheDate(theDate);
                    List<DictWorkDate> lsDate = getList(dictWorkDateDto);
                    if (lsDate.size() == 0) {
                        DictWorkDate dictWorkDate = new DictWorkDate();
                        dictWorkDate.setTheDate(theDate);
                        dictWorkDate.setFlag("1");
                        dictWorkDate.setMemo("周末");
                        list.add(dictWorkDate);
                    }
                }
            }
        }
        for (DictWorkDate dictWorkDate2 : list) {
            baseMapper.insert(dictWorkDate2);
        }
        return true;
    }
}
